/*
 * @Author: LVGRAPE
 * @LastEditors: LVGRAPE
 */
#include "Arduino.h"
#include "at32F4xx.h"
#include "delay.h"
#include "RCC_Configuration.h"
#include "USART_Configuration.h"
#include "NVIC_Configuration.h"
#include "GPIO_Configuration.h"
#include "IAP.h"
#include <stdio.h>
#include "AT32Models.h"

AT32_MCU_TYPE whoAmI;


int main()
{
	SystemInit();
	SCB->VTOR = FLASH_BASE;
	RCC_Configuration();
	GPIO_Configuration();
	NVIC_Configuration();
	USART1_Begin(2000000);
	// SysTick_Init(150-1);/*Systick clock 150MHz, 150 ticks is 1us*/
	SysTick_Init(SystemCoreClock/1000);/*Systick clock 150MHz, 150 ticks is 1us*/
	at32_mcu_who_am_i(&whoAmI);//读取芯片型号
#ifdef ARDUINO
	setup();
#endif // ARDUINO
	// char str[10];
	// usart_sendBytes(myitoa(SystemCoreClock,str,10));
	// usart_sendBytes("at32f413 isp test\n");
	// printf("SystemCoreClock:%d\n",SystemCoreClock);
	// FLASH_RW_TEST();
	// AT32_MCU_TYPE whoAmI;
	// at32_mcu_who_am_i(&whoAmI);
	// rt_kprintf("Serial:%d\t",whoAmI.Serial);
	// rt_kprintf("id:%d\t",whoAmI.ID);
	// usart_sendBytes(at32_mcu_mode2str(&whoAmI));usart_sendBytes("\t");
	// usart_sendBytes(at32_mcu_flash2str(&whoAmI));usart_sendBytes("\t");
	// usart_sendBytes(at32_mcu_footprint2str(&whoAmI));usart_sendBytes("\n");
	while(1)
	{
		// FLASH_RW_TEST();
		// usart_sendBytes("\n\nsystick_cnt_ms:");
		// usart_sendBytes(myitoa(systick_cnt_ms,str,10));
		// usart_sendBytes("at32f413 isp test\n");
		// delay_ms(100);
		IAP_Handle();//IAP downloader

#ifdef ARDUINO
		loop();
#endif // ARDUINO

	}
}
